package com.xh.rufongwallpaper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xh.rufongwallpaper.entity.DownloadRecord;
import com.xh.rufongwallpaper.mapper.DownloadRecordMapper;
import com.xh.rufongwallpaper.service.DownloadService;
import com.xh.rufongwallpaper.service.WallpaperService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 下载服务实现类
 *
 * @author XH
 */
@Service
@RequiredArgsConstructor
public class DownloadServiceImpl implements DownloadService {

    private final DownloadRecordMapper downloadRecordMapper;
    private final WallpaperService wallpaperService;

    @Override
    @Transactional
    public DownloadRecord recordDownload(String deviceId, Long wallpaperId) {
        // 增加壁纸下载计数
        wallpaperService.incrementDownloadCount(wallpaperId);

        // 创建下载记录
        DownloadRecord downloadRecord = new DownloadRecord();
        downloadRecord.setDeviceId(deviceId);
        downloadRecord.setWallpaperId(wallpaperId);
        
        downloadRecordMapper.insert(downloadRecord);
        return downloadRecord;
    }

    @Override
    public Page<DownloadRecord> getDownloadsByDeviceId(String deviceId, int pageIndex, int pageSize) {
        Page<DownloadRecord> page = new Page<>(pageIndex + 1, pageSize);
        return downloadRecordMapper.selectPage(page,
                new LambdaQueryWrapper<DownloadRecord>()
                        .eq(DownloadRecord::getDeviceId, deviceId)
                        .orderByDesc(DownloadRecord::getCreateTime)
        );
    }

    @Override
    public Page<DownloadRecord> getDownloadsByWallpaperId(Long wallpaperId, int pageIndex, int pageSize) {
        Page<DownloadRecord> page = new Page<>(pageIndex + 1, pageSize);
        return downloadRecordMapper.selectPage(page,
                new LambdaQueryWrapper<DownloadRecord>()
                        .eq(DownloadRecord::getWallpaperId, wallpaperId)
                        .orderByDesc(DownloadRecord::getCreateTime)
        );
    }

    @Override
    public long countDownloadsByDeviceId(String deviceId) {
        return downloadRecordMapper.selectCount(
                new LambdaQueryWrapper<DownloadRecord>()
                        .eq(DownloadRecord::getDeviceId, deviceId)
        );
    }

    @Override
    public long countDownloadsByWallpaperId(Long wallpaperId) {
        return downloadRecordMapper.selectCount(
                new LambdaQueryWrapper<DownloadRecord>()
                        .eq(DownloadRecord::getWallpaperId, wallpaperId)
        );
    }

    @Override
    public long countDownloadsBetween(Date startTime, Date endTime) {
        return downloadRecordMapper.selectCount(
                new LambdaQueryWrapper<DownloadRecord>()
                        .between(DownloadRecord::getCreateTime, startTime, endTime)
        );
    }

    @Override
    public boolean hasDownloaded(String deviceId, Long wallpaperId) {
        return downloadRecordMapper.exists(
                new LambdaQueryWrapper<DownloadRecord>()
                        .eq(DownloadRecord::getDeviceId, deviceId)
                        .eq(DownloadRecord::getWallpaperId, wallpaperId)
        );
    }
} 